package com.yashi.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yashi.entity.TbAdmin;
import com.yashi.service.AdminService;
import com.yashi.service.UserService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Resource
	private AdminService adminService;
	@Resource
	private UserService userService;
	
	@RequestMapping("/gologin")
	public String goLogin(){
		return "login";
	}

	/**
	 * 管理员登录
	 * @param admin
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String findAdminByName(TbAdmin admin,HttpSession session,HttpServletRequest request){
		try {
			TbAdmin findAdmin= adminService.findAdminByName(admin);
			session.setAttribute("admin", findAdmin);
			session.setMaxInactiveInterval(-1);
			return "adminMain";
		} catch (Exception e) {
			request.setAttribute("error", "用户名或密码错误");
			return "login";
		}
		
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.removeAttribute("user");
		return "login";
	}
	
	/**
	 * 增加管理员
	 * @param userId
	 * @return
	 */
	@RequestMapping("/addAdmin")
	public String addAdmin(TbAdmin admin,String userId){
		adminService.addAdmin(admin,userId);
		return "redirect:/user/findUserList?pageNum=1";
		
		
	}
	
}
